package com.example.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.util.Result;
import com.example.dto.system.RouterDto;
import com.example.springsecurity.custom.LoginUserInfoHelper;
import com.example.system.SysDept;
import com.example.system.SysPost;
import com.example.system.SysUser;
import com.example.system.mapper.SysUserMapper;
import com.example.system.service.SysDeptService;
import com.example.system.service.SysMenuService;
import com.example.system.service.SysPostService;
import com.example.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysPostService sysPostService;

    /**
     * 分页查询用户列表
     * @return
     */
    public Result getPageList(int page, int limit, String keyword, String createTimeBegin, String createTimeEnd){
        if(page == 0 || page < 0){
            page = 1;
        }
        if (limit == 0 || limit < 0 || limit > 20){
            limit = 10;
        }
        Page ipage = new Page();
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isBlank(keyword)) {
            wrapper.like(SysUser::getName, keyword);
        }
        if (!(StringUtils.isBlank(createTimeBegin) && StringUtils.isBlank(createTimeEnd))) {
            wrapper.between(SysUser::getCreateTime, createTimeBegin, createTimeEnd);
        }
        if (StringUtils.isBlank(createTimeBegin) && !StringUtils.isBlank(createTimeEnd)) {
            wrapper.ge(SysUser::getCreateTime, createTimeBegin);
        }
        if (!StringUtils.isBlank(createTimeBegin) && !StringUtils.isBlank(createTimeEnd)) {
            wrapper.le(SysUser::getCreateTime, createTimeEnd);
        }

        Page page1 = page(ipage, wrapper);
        return Result.ok(page1);
    }

    /**
     * 获取用户
     * @param username
     * @return
     */
    @Override
    public SysUser getByUsername(String username) {
//        if(StringUtils.isBlank(username)){
//            return Result.fail("用户名为空");
//        }
        SysUser user = getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername, username));
        return user;
    }

    /**
     * 根据用户名获取用户登录信息
     * @param username
     * @return
     */
    @Override
    public Map<String, Object> getUserInfo(String username) {
        Map<String,Object> result = new HashMap<>();
        SysUser sysUser = this.getByUsername(username);

        //根据用户id获取菜单权限值
        List<RouterDto> routerDtoList = sysMenuService.findUserMenuList(sysUser.getId());
        //根据用户id获取用户按钮权限
        List<String> permsList = sysMenuService.findUserPermsList(sysUser.getId());

        result.put("name",sysUser.getName());
        result.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        //当前权限控制使用不到，我们暂时忽略
        result.put("roles",new HashSet<>());
        result.put("buttons",permsList);
        result.put("routers",routerDtoList);
        return result;
    }

    /**
     * 我的基本信息
     * @return
     */
    @Override
    public Map<String, Object> getCurrentUser() {
        SysUser sysUser = baseMapper.selectById(LoginUserInfoHelper.getUserId());
        SysDept sysDept = sysDeptService.getById(sysUser.getDeptId());
        SysPost sysPost = sysPostService.getById(sysUser.getPostId());
        Map<String,Object> map = new HashMap<>();
        map.put("name",sysUser.getName());
        map.put("phone",sysUser.getPhone());
        return map;
    }

}
